package common;

import android.app.Application;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.TypedValue;

import com.laoxinwen.app.R;
import com.sina.weibo.sdk.utils.ResourceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sshong on 15/6/16.
 */
public class MyTheme {
    public static final int THEME_NIGHT = 1;
    public static final int THEME_DEFAULT = 2;
    public static final int THEME_RED = 3;
    public static final int THEME_PINK = 4;
    public static final int THEME_PURPLE = 5;
    public static final int THEME_INDIGO = 6;
    public static final int THEME_TEAL = 7;
    public static final int THEME_GREEN = 8;
    public static final int THEME_ORANGE = 9;

    private static List<MyTheme> totalThemes = new ArrayList<MyTheme>();
    private static Map<String, MyTheme> themeMap = new HashMap<String, MyTheme>();

    private int id;
    private String name;

    public MyTheme(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public static void init() {
        if (totalThemes.isEmpty()) {
            addTheme(THEME_NIGHT, "夜间");
            addTheme(THEME_DEFAULT, "默认");
            addTheme(THEME_RED, "红色");
            addTheme(THEME_PINK, "粉红");
            addTheme(THEME_PURPLE, "紫色");
            addTheme(THEME_INDIGO, "靛蓝");
            addTheme(THEME_TEAL, "蓝绿");
            addTheme(THEME_GREEN, "绿色");
            addTheme(THEME_ORANGE, "橙色");
        }
    }

    private static void addTheme(int id, String name) {
        MyTheme theme = new MyTheme(id, name);
        totalThemes.add(theme);
        themeMap.put(String.valueOf(id), theme);
    }

    public static List<MyTheme> getTotalThemes() {
        return totalThemes;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public static MyTheme getTheme(int id) {
        return themeMap.get(String.valueOf(id));
    }

    public int getThemeResId() {
        int theme = R.style.AppTheme_Default;
        switch (id) {
            case MyTheme.THEME_NIGHT:
                theme = R.style.AppTheme_Night;
                break;
            case MyTheme.THEME_DEFAULT:
                theme = R.style.AppTheme_Default;
                break;
            case MyTheme.THEME_RED:
                theme = R.style.AppTheme_Red;
                break;
            case MyTheme.THEME_PINK:
                theme = R.style.AppTheme_Pink;
                break;
            case MyTheme.THEME_PURPLE:
                theme = R.style.AppTheme_Purple;
                break;
            case MyTheme.THEME_INDIGO:
                theme = R.style.AppTheme_Indigo;
                break;
            case MyTheme.THEME_TEAL:
                theme = R.style.AppTheme_Teal;
                break;
            case MyTheme.THEME_GREEN:
                theme = R.style.AppTheme_Green;
                break;
            case MyTheme.THEME_ORANGE:
                theme = R.style.AppTheme_Orange;
                break;
        }
        return theme;
    }

    public int getMainColor() {
        return getThemeColor(R.attr.theme_main_color);
    }

    public int getThemeColor(int attr) {
        TypedValue typedValue = new TypedValue();
        Resources.Theme theme = MyApp.getContext().getResources().newTheme();
        theme.applyStyle(getThemeResId(), true);
        theme.resolveAttribute(attr, typedValue, true);
        int color = typedValue.data;
        return color;
    }

    @Override
    public String toString() {
        return name;
    }
}
